﻿using System;
using System.Collections.Generic;
using MAF.Common;

namespace MAF.UserSecurity.Repository
{
    /// <summary>
    /// 用户存储管理接口
    /// </summary>
    public class UserRepo
    {
        #region 单例

        /// <summary>
        /// 私有构造方法
        /// </summary>
        private UserRepo() 
        {
            #if DEBUG
            
            var admin = new User();
            admin.Name = "admin";
            admin.Password = "123456";
            _Users.Add(admin);

            #endif
        }

        /// <summary>
        /// 单例属性变量
        /// </summary>
        private static UserRepo _instance = null;

        /// <summary>
        /// 单例属性
        /// </summary>
        public static UserRepo Instance 
        { 
            get 
            {
                if (null == _instance)
                    _instance = new UserRepo();

                return _instance; 
            } 
        }

        #endregion

        /// <summary>
        /// 存储新用户
        /// </summary>
        /// <param name="info"></param>
        public virtual void  Add(User info)
        {
            if (null == info)
                throw new ArgumentNullException();

            _Users.Add(info);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="info"></param>
        public virtual void  Update(User info)
        {
            if (null == info)
                throw new ArgumentNullException();

            int index = _Users.FindIndex(delegate(User entity)
            {
                if (entity.Id == info.Id)
                    return true;
                else
                    return false;
            });

            _Users[index] = info;
        }

        /// <summary>
        /// 移除用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public virtual int  Remove(Guid id)
        {
            var q = new Query<User>();
            q.AddCriterion("Id", Op.Equals, id);
            return RemoveByQuery(q);
        }

        /// <summary>
        /// 移除用户
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public virtual int RemoveByQuery(Query<User> q)
        {
            return _Users.RemoveAll(delegate(User entity) 
            {
                foreach (Criterion<User> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 查询用户明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual User GetById(Guid id)
        {
            Query<User> q = new Query<User>();
            q.AddCriterion("Id", Op.Equals, id);
            var list = GetByQuery(q);
            if (0 == list.Count)
                return null;
            else
                return list[0];
        }

        /// <summary>
        /// 查询用户清单
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public virtual List<User> GetByQuery(Query<User> q)
        {
            if (null == q)
                throw new ArgumentNullException();

            return _Users.FindAll(delegate(User entity) 
            {
                foreach (Criterion<User> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 查询用户清单的数量
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public virtual int CountByQuery(Query<User> q)
        {
            return GetByQuery(q).Count;
        }

        /// <summary>
        /// 用户内存存储器
        /// </summary>
        private static List<User> _Users = new List<User>();

    }
}
